package cn.iocoder.yudao.module.trade.service.order;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.api.address.MemberAddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.MemberAddressRespDTO;
import cn.iocoder.yudao.module.trade.api.tradedelivery.dto.TradeOrderDeliveryDTO;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDeliveryDO;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderDeliveryMapper;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryPickUpStoreService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 订单配送信息(TradeOrderDelivery)表服务实现类
 *
 * @author makejava
 * @since 2025-03-22 07:17:49
 */
@Service
public class TradeOrderDeliveryServiceImpl implements TradeOrderDeliveryService {
    @Resource
    private TradeOrderDeliveryMapper tradeOrderDeliveryMapper;
    @Resource
    private MemberAddressApi addressApi;
    @Resource
    private DeliveryPickUpStoreService pickUpStoreService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TradeOrderDeliveryDO queryById(Long id) {
        return this.tradeOrderDeliveryMapper.selectById(id);
    }


    /**
     * 新增数据
     *
     * @param tradeOrderDeliveryDO 实例对象
     * @return 实例对象
     */
    @Override
    public TradeOrderDeliveryDO insert(TradeOrderDeliveryDO tradeOrderDeliveryDO) {
        this.tradeOrderDeliveryMapper.insert(tradeOrderDeliveryDO);
        return tradeOrderDeliveryDO;
    }

    /**
     * 修改数据
     *
     * @param tradeOrderDeliveryDO 实例对象
     * @return 实例对象
     */
    @Override
    public TradeOrderDeliveryDO update(TradeOrderDeliveryDO tradeOrderDeliveryDO) {
        this.tradeOrderDeliveryMapper.updateById(tradeOrderDeliveryDO);
        return this.queryById(tradeOrderDeliveryDO.getId());
    }

    @Override
    public List<TradeOrderDeliveryDO> queryByOrderNo(String orderNo) {
        return tradeOrderDeliveryMapper.selectList(new LambdaQueryWrapperX<TradeOrderDeliveryDO>()
                .eq(TradeOrderDeliveryDO::getOrderNo, orderNo));
    }

    @Override
    public TradeOrderDeliveryDO queryByOrderNoAndType(String orderNo, DeliveryTypeEnum deliveryTypeEnum) {
        List<TradeOrderDeliveryDO> res = tradeOrderDeliveryMapper.selectList(TradeOrderDeliveryDO::getOrderNo, orderNo,
                TradeOrderDeliveryDO::getType,deliveryTypeEnum.getType());
        return res!= null && res.size() > 0 ? res.get(0) : null;

    }

    @Override
    public void createPickUpDelivery(Long userId, Long id, Integer deliveryType, Long pickUpStoreId, Long addressId, Long pickupInfoId) {
        if (DeliveryTypeEnum.PICK_UP.getType().equals(deliveryType)) {
            DeliveryPickUpStoreDO storeAddress = pickUpStoreService.getDeliveryPickUpStore(pickUpStoreId);
            TradeOrderDeliveryDO deliveryDO = TradeOrderConvert.INSTANCE.convertDelivery(storeAddress, String.valueOf(id));
            deliveryDO.setPickupInfoId(pickupInfoId);
            deliveryDO.setType(DeliveryTypeEnum.PICK_UP.getType());
            deliveryDO.setId(null);
            tradeOrderDeliveryMapper.insert(deliveryDO);
        } else {
            MemberAddressRespDTO address = addressApi.getAddress(addressId, userId);
            Assert.notNull(address, "地址({}) 不能为空", addressId);
            TradeOrderDeliveryDO deliveryDO = TradeOrderConvert.INSTANCE.convertDelivery(address, String.valueOf(id));
            deliveryDO.setType(DeliveryTypeEnum.getEnum(deliveryType).getType());
            deliveryDO.setPickupInfoId(address.getPickupInfoId());
            deliveryDO.setId(null);
            tradeOrderDeliveryMapper.insert(deliveryDO);
        }
    }

    @Override
    public TradeOrderDeliveryDTO queryByWithdrawId(Long id) {
        return TradeOrderConvert.INSTANCE.convertDeliveryDTO(tradeOrderDeliveryMapper.selectOne(TradeOrderDeliveryDO::getOrderNo, String.valueOf(id)));
    }
}
